<?php
/**
 * User: 徐剑锋
 * Date: 2016/11/16
 * Time: 18:23
 */
namespace admin;

use C\lib\Db;
use C\lib\Sql;
use C\mod\ads;
use C\mod\auth;

class auths extends ctr {

    // --- 权限列表 ---
    public function authList() {
        if(auths::canAuth()) {

            $a = auth::getList([]);

            $jauth = []; $wids = [];
            foreach($a as $v) {

                $jauth[] = [
                    'id'=>$v->id,
                    'ads_ids'=>$v->ads_ids,
                    'comment'=>$v->comments,
                    'time_add'=>$v->time_add
                ];
                $wids = array_merge($wids,explode(',',$v->ads_ids));
            }

            $wids = array_unique($wids);

            $ads=ads::getList([['id','in',$wids]]);

            foreach($ads as $a) {
                $j[$a->id]  =$a->realname;

            }


            $this->render('auths/main', [
                'jauth'   => $jauth,
                 'j' => $j
            ]);
        }else
            $this->render('user1/main',['err'=>'未登陆或无此权限']);
    }


    // --- 更新权限 ---
    public function updateAuth() {

        if(auths::canAuth()) {

            if(isset($_POST['inputAid']) && trim($_POST['inputAid'])!='' && isset($_POST['chk']) && count($_POST['chk'])) {
                date_default_timezone_set('PRC');

                $r = Db::prepare('UPDATE ' . DB_PRE . 'auth SET worker_ids = :worker_ids, time_add = :time_add WHERE id = :id');
                $r->execute([
                    ':worker_ids' => implode(',' , $_POST['chk']),
                    ':time_add' => $_SERVER['REQUEST_TIME'],
                    ':id' => trim($_POST['inputAid'])
                ]);

                if($r && $r->rowCount()>0) {
                    $this->writeJson( 1 , [] ,1 );
                }else
                    $this->writeJson( 0 , '更新权限表失败' ,1 );
            }else
                $this->writeJson( 0 , '参数传递错误' ,1 );
        }else
            $this->writeJson( -1 , '未登陆或无此权限' ,1 );

    }
}
